package com.iseastar.guojiang.view;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.shouhuobao.station.R;

/**
 * 该类主要封装 app tab的切换效果
 *
 * @author coffee <br>
 * 2016-1-15下午12:00:55
 */
public class AppTab1 implements View.OnClickListener {

    private int mPositionTwo;
    private int currentIndex;

    private Activity context;

    private ViewPager mViewPager;
    private TextView[] mTags;
    private View mImgLine;

    private TabChangeCallback callback;

    private int margin = 0;

    private int selectedColor;
    private int unSelectedColor;

    private int widthMargin = 0;

    private int lineWidth = 0;

    public int getMargin() {
        return margin;
    }

    public void setMargin(int margin) {
        this.margin = margin;
    }

    public interface TabChangeCallback {
        public void onTabChanage(int whitch);
    }

    public void setCallback(TabChangeCallback callback) {
        this.callback = callback;
    }

    public AppTab1(Activity context) {
        this.context = context;
        selectedColor = context.getResources().getColor(R.color.app_font_green);
        unSelectedColor = context.getResources().getColor(R.color.app_font_blue);
    }

    public AppTab1(Activity context, int margin) {
        this.context = context;
        this.margin = margin;
        selectedColor = context.getResources().getColor(R.color.app_font_green);
        unSelectedColor = context.getResources().getColor(R.color.app_font_blue);
    }

    public void setWidthMargin(int widthMargin) {
        this.widthMargin = widthMargin;
    }

    public void setTextColor(int selectedColor, int unSelectedColor) {
        this.selectedColor = selectedColor;
        this.unSelectedColor = unSelectedColor;
    }

    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
    }

    /**
     * @param viewPager
     * @param line
     * @param tags
     */
    public void initPosition(ViewPager viewPager, View line, TextView[] tags) {
        this.mViewPager = viewPager;
        this.mImgLine = line;
        this.mTags = tags;
        for (TextView tag : mTags) {
            tag.setOnClickListener(this);
        }
        mViewPager.addOnPageChangeListener(new MyOnPageChangeListener());
        mViewPager.setCurrentItem(0, true);
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mImgLine.getLayoutParams();
        params.leftMargin = margin;
        params.rightMargin = margin;
        params.gravity = Gravity.BOTTOM;
        params.width = (screenW - widthMargin) / mTags.length - params.leftMargin - params.rightMargin;
        mImgLine.setLayoutParams(params);

        mPositionTwo = (screenW - widthMargin) / mTags.length;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        // if (id == mTags[0].getId()) {
        // mViewPager.setCurrentItem(0, true);
        // mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_1));
        // mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_2));
        // } else if (id == mTags[1].getId()) {
        // mViewPager.setCurrentItem(1, true);
        // mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_2));
        // mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_1));
        // }
        for (int i = 0; i < mTags.length; i++) {
            if (id == mTags[i].getId()) {
                mViewPager.setCurrentItem(i, true);
            }
        }
    }

    /**
     * 设置当前选中的tab
     *
     * @param position 从0开始
     */
    public void selectTab(int position) {
        onClick(mTags[0]);
    }

    private class MyOnPageChangeListener implements OnPageChangeListener {

        public void onPageSelected(int arg0) {
            Animation animation = null;
            // switch (arg0) {
            // case 0:
            // if (currentIndex == 1) {
            // animation = new TranslateAnimation(mPositionTwo, 0, 0, 0);
            // }
            // mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_1));
            // mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_2));
            // break;
            // case 1:
            // if (currentIndex == 0) {
            // animation = new TranslateAnimation(0, mPositionTwo, 0, 0);
            // }
            // mTags[0].setTextColor(context.getResources().getColor(R.color.app_font_2));
            // mTags[1].setTextColor(context.getResources().getColor(R.color.app_font_1));
            // break;
            // }

            animation = new TranslateAnimation(currentIndex * mPositionTwo, arg0 * mPositionTwo, 0, 0);
            for (int i = 0; i < mTags.length; i++) {
                if (i == arg0) {
                    mTags[i].setTextColor(selectedColor);
                } else {
                    mTags[i].setTextColor(unSelectedColor);
                }
            }
            currentIndex = arg0;
            if (callback != null) {
                callback.onTabChanage(currentIndex);
            }
            animation.setFillAfter(true);
            animation.setDuration(300);
            mImgLine.startAnimation(animation);
        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
